package com.xjh.him.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xjh.him.common.LoginUser;
import com.xjh.him.dao.UserGroupMemberMapper;
import com.xjh.him.domain.UserDO;
import com.xjh.him.domain.UserGroupMemberDO;
import com.xjh.him.dto.usergroupmember.UserGroupMemberDTO;
import com.xjh.him.dto.usergroupmember.UserGroupMemberInviteDTO;
import com.xjh.him.netty.NettyCore;
import com.xjh.him.netty.codec.Command;
import com.xjh.him.netty.codec.Packet;
import com.xjh.him.netty.dto.UserGroupMemberInviteNotifyDTO;
import io.netty.channel.Channel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xjh.tool.Checks;
import org.xjh.tool.common.ErrorMsg;
import org.xjh.tool.dto.ResultDTO;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserGroupMemberServiceImpl implements IUserGroupMemberService {

    @Autowired
    private UserGroupMemberMapper userGroupMemberMapper;


    @Override
    public ResultDTO<List<UserGroupMemberDTO>> listMember(Long groupId) {
        if (Checks.isEmpty(groupId)) {
            return ResultDTO.getFailureResult(ErrorMsg.ERR_PARAMETER_IS_NULL);
        }
        QueryWrapper<UserGroupMemberDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_group_id", groupId);
        List<UserGroupMemberDO> memberDOList = userGroupMemberMapper.selectList(queryWrapper);
        List<UserGroupMemberDTO> dtoList = memberDOList.stream().map(item -> {
            UserGroupMemberDTO dto = new UserGroupMemberDTO();
            BeanUtils.copyProperties(item, dto);
            return dto;
        }).collect(Collectors.toList());
        return ResultDTO.getSuccessResult(dtoList);
    }

    @Override
    public ResultDTO<Boolean> inviteMember(UserGroupMemberInviteDTO inviteDTO) {
        if (Checks.isAnyEmpty(inviteDTO, inviteDTO.getUserGroupId(), inviteDTO.getUserList())) {
            return ResultDTO.getFailureResult(ErrorMsg.ERR_PARAMETER_IS_NULL);
        }
        List<UserGroupMemberDO> doList = inviteDTO.getUserList().stream().map(item -> {
            UserGroupMemberDO memberDO = new UserGroupMemberDO();
            memberDO.setUserGroupId(inviteDTO.getUserGroupId());
            BeanUtils.copyProperties(item, memberDO);
            return memberDO;
        }).collect(Collectors.toList());
        int insert = userGroupMemberMapper.insertBatch(doList);
        if (insert == 0) {
            ResultDTO.getFailureResult(ErrorMsg.ERR_SYSTEM_BUSY);
        }
        UserDO loginUser = LoginUser.getLoginUser();
        QueryWrapper<UserGroupMemberDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_group_id", inviteDTO.getUserGroupId());
        List<UserGroupMemberDO> memberDOList = userGroupMemberMapper.selectList(queryWrapper);
        List<UserGroupMemberInviteNotifyDTO.InviteUser> inviteUserList = inviteDTO.getUserList().stream()
                .map(item -> {
            UserGroupMemberInviteNotifyDTO.InviteUser inviteUser = new UserGroupMemberInviteNotifyDTO.InviteUser();
            inviteUser.setUserId(item.getUserId());
            inviteUser.setUserNickname(item.getUserNickname());
            return inviteUser;
        }).collect(Collectors.toList());
        for (UserGroupMemberDO item : memberDOList) {
            Channel channel = NettyCore.ACTIVE_USER_CHANNEL.get(item.getUserId());
            if (channel != null) {
                UserGroupMemberInviteNotifyDTO notifyDTO = new UserGroupMemberInviteNotifyDTO();
                notifyDTO.setMemberId(loginUser.getId());
                notifyDTO.setUserList(inviteUserList);
                channel.writeAndFlush(
                        Packet.create(Command.USER_GROUP_MEMBER_INVITE_NOTIFY, notifyDTO)
                );
            }
        }
        return ResultDTO.getSuccessResult(true);
    }
}
